package org.example.controller;

import org.example.mapper.UserMapper;
import org.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    //注入Spring容器中的UserMapper对象
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/all")
    public Object all(){
        List<User> userList = userMapper.getAll();
        return userList;
    }

    @RequestMapping("/insert")
    public Object insert(User user){
        int n = userMapper.insertOne(user);
        return user;
    }

    @RequestMapping("/update")
    public Object update(User user){
        int n = userMapper.updateOne(user);
        return user;
    }

    @RequestMapping("/delete")
    public Object delete(Integer id){
        int n = userMapper.deleteById(id);
        return n;
    }

    @RequestMapping("/like")
    public Object like(String username){
        List<User> l = userMapper.selectLike(username);
        return l;
    }

    @RequestMapping("/orderBy")
    public Object orderBy(String orderBy){
        List<User> l = userMapper.selectOrderBy(orderBy);
        return l;
    }

    @RequestMapping("/rel/user")
    public Object relUser(){
        List<User> l = userMapper.selectUserAndArticle();
        return l;
    }

    @RequestMapping("/condition")
    public Object condition(User user){
        List<User> l = userMapper.selectByCondition(user);
        return l;
    }

    @RequestMapping("/deleteByIds")
    public Object deleteByIds(@RequestParam List<Integer> ids){
        int n = userMapper.deleteByIds(ids);
        return n;
    }
}
